package com.wunu.smartlock1.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wunu.smartlock1.R;

/**
 * Created by admin on 2018/4/21.
 */

public class LockListActivity extends AppCompatActivity implements OnClickListener{
    View inflate;
    Dialog dialog;
    Intent intent = new Intent();
    Gson gson = new Gson();
    ImageView addButton;//添加新锁
    ImageView memberButton;//家庭成员
    ImageView temporaryKeyButton;//临时秘钥
    ImageView authorizedButton;//授权成员
    ImageView lockEventButton;//开锁记录
    ImageView lockQRCodeButton;//锁二维码
    ImageView statusImageView;//开锁按钮

    TextView cancelView;//取消
    TextView resetView;//重置秘钥
    TextView stopView;//停用蓝牙解锁
    TextView deleteView;//删除门锁
    private Class lzContext;
    private String toast;//弹框提示
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            System.out.println("msg----------------------->:"+msg);
            System.out.println("obj->"+msg.getData());
            if(msg.obj == null){
                Toast.makeText(LockListActivity.this,"取"+toast+"失败",Toast.LENGTH_SHORT).show();
                return;
            }
            System.out.println("obj:"+msg.obj);
            Toast.makeText(LockListActivity.this,"取"+toast+"成功",Toast.LENGTH_SHORT).show();
            intent.putExtra("param",gson.toJson(msg.obj));
            startActivity(intent.setClass(LockListActivity.this, lzContext));
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_lock_list);

        addButton = findViewById(R.id.addButton);
        memberButton = findViewById(R.id.memberButton);
        temporaryKeyButton = findViewById(R.id.temporaryKeyButton);
        authorizedButton = findViewById(R.id.authorizedButton);
        lockEventButton = findViewById(R.id.lockEventButton);
        lockQRCodeButton = findViewById(R.id.lockQRCodeButton);
        statusImageView = findViewById(R.id.statusImageView);

        //给button按钮绑定单击事件，设置完参数后跳转到首页

        addButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("添加新锁", "list 1:");
                System.out.println("添加新锁");
                startActivity(intent.setClass(LockListActivity.this, ScanListActivity.class));
            }
        });

        memberButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("家庭成员", "list 1:");
                memberShow();
            }
        });
        temporaryKeyButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("临时秘钥", "list 1:");
                startActivity(intent.setClass(LockListActivity.this, TemporyLockActivity.class));
            }
        });
        authorizedButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("授权成员", "list 1:");
                startActivity(intent.setClass(LockListActivity.this, AuthorizedLockActivity.class));
            }
        });
        lockEventButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("开锁记录", "list 1:");
                startActivity(intent.setClass(LockListActivity.this, LockRecordActivity.class));
            }
        });

        lockQRCodeButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.v("锁二维码", "list 1:");
                startActivity(intent.setClass(LockListActivity.this, QrcodeActivity.class));
            }
        });
        statusImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("开锁", "点击开锁");
            }
        });
    }
    public void memberShow(){
        dialog = new Dialog(this,R.style.smartlock_ActionSheetDialogStyle);
        inflate = LayoutInflater.from(this).inflate(R.layout.smartlock_buttom_dialog, null);


        cancelView = inflate.findViewById(R.id.cancel);//取消弹框
        resetView = inflate.findViewById(R.id.resetlock);//取消弹框
        deleteView = inflate.findViewById(R.id.deletelock);//取消弹框
        stopView = inflate.findViewById(R.id.stoplock);//取消弹框
        resetView.setOnClickListener(this);
        cancelView.setOnClickListener(this);
        deleteView.setOnClickListener(this);
        stopView.setOnClickListener(this);
        dialog.setContentView(inflate);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity( Gravity.BOTTOM);
       /* WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;
        dialogWindow.setAttributes(lp);*/
        dialog.show();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.cancel){
            dialog.dismiss();
        }else if(id == R.id.resetlock){
            Toast.makeText(this,"点击了重置秘钥按钮",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.deletelock){
            Toast.makeText(this,"点击了删除门锁按钮",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.stoplock){
            Toast.makeText(this,"点击了停用蓝牙解锁按钮",Toast.LENGTH_SHORT).show();
        }
        //dialog.dismiss();
    }
}